<?php
/**
 * 带奖励的群发邮件
 */

require_once('../inc/config.php');
require_once ROOT_PATH .'/utility/class/socket.class.php';
//权限判断
chkpurview('group_email');

if (isset($_POST['send'])){
	$title = trim($_POST['emailtitle']);
	if (!$title)
	{
		 gourl($_LANG['fill_incomplete'], '', -1);
	}
	elseif(strlen($title) > 36)
	{
		 gourl($_LANG['email_title_too_long'], '', -1);
	}
	 
	$min_level = (int)trim($_POST['min_level']);
	$max_level = (int)trim($_POST['max_level']);
	if (!$min_level || !$max_level) gourl($_LANG['fill_incomplete'], '', -1);
	
	$timing = strtotime(trim($_POST['timing']));
	
	$content = stripslashes(trim($_POST['content']));
	if (!$content) gourl($_LANG['fill_incomplete'], '', -1);
	
	$linkArray = array();
	$contentArray = array();
	$tmp = @match_links($content);						//提取邮件内容中的链接
	
	$content = strip_tags($content);
	$content = preg_replace("/[\s]{2,}/","",$content);
	foreach($tmp['content'] as $key=>$val)				//将链接替换成html格式
	{
		$content = str_replace($val, '<FONT COLOR="#00FF00"><A HREF="'.$tmp['link'][$key].'" TARGET="_blank"><U>'.$val.'</U></A></FONT>', $content);
	}
	
	$p = $_POST['profession'];
	if (!$p) gourl($_LANG['fill_incomplete'], '', -1);
	$profession = '00000000';
	foreach ($p as $v){
		//组合成二进制数
		$profession = substr_replace($profession, '1', $v-1, 1);
	}
	$profession = bindec(strrev($profession));	//转成十进制数
	
	$isBind = isset($_POST['isBind'])?$_POST['isBind']:null;	//0不绑定,1绑定(默认)
	$itemtype = $_POST['itemtype'];
	$itemnum = $_POST['itemnum'];
	$itemid = $_POST['itemid'];
	if (!$itemtype || !$itemnum) gourl($_LANG['fill_incomplete'], '', -1);
	$rewards = NULL;
	
	foreach ($itemtype as $k => $v){
		if ($v == 0) continue;
		elseif ($v == 1){//道具
			if (!$itemid[$k] || !$itemnum[$k]) gourl($_LANG['fill_incomplete'], '', -1);
			$temp['type'] = 0x2008;
			$temp['value'] = intval($itemid[$k]<<16)+ intval($isBind[$k]<<8) + intval($itemnum[$k]);
			$rewards[] = $temp;
		}elseif ($v == 2){//元宝
			if (!$itemnum[$k]) gourl($_LANG['fill_incomplete'], '', -1);
			$temp['type'] = 0x1006;
			$temp['value'] = $itemnum[$k];
			$rewards[] = $temp;
		}elseif ($v == 3){//礼金
			if (!$itemnum[$k]) gourl($_LANG['fill_incomplete'], '', -1);
			$temp['type'] = 0x1007;
			$temp['value'] =  $itemnum[$k];
			$rewards[] = $temp;
		}elseif ($v == 4){//铜币
			if (!$itemnum[$k]) gourl($_LANG['fill_incomplete'], '', -1);
			$temp['type'] = 0x1004;
			$temp['value'] =  $itemnum[$k];
			$rewards[] = $temp;
		}elseif ($v == 5){//绑定铜币
			if (!$itemnum[$k]) gourl($_LANG['fill_incomplete'], '', -1);
			$temp['type'] = 0x1005;
			$temp['value'] =  $itemnum[$k];
			$rewards[] = $temp;
		}
	}
	
	/*--------------插入数据库信息--------------*/
	$oper_type = 1;
	if ($rewards){
		foreach ($rewards as $v){
			$rewards_temp[] = $v['type'].'.'.$v['value'];
		}
		$rewards_value = implode(":", $rewards_temp);
		unset($rewards_temp);
	}else $rewards_value = NULL;
	$oper_value = $title.'#'.$profession.'#'.$min_level.'#'.$max_level.'#'.$timing.'#'.$content.'#'.$rewards_value;
	/*-----------------------------------------*/
	save_adminoper($oper_type, $oper_value, 1);				//1：待审核
	gourl($_LANG['operate_success'], 'email.php');
}

$giftArray = array(1,2,3,4,5,6,7);
$arrRes = array(
			0 => $_LANG['none'],
			1 => $_LANG['daoju'],
			2 => $_LANG['gold'],
			3 => $_LANG['bind_gold'],
			4 => $_LANG['copper'],
			5 => $_LANG['bind_copper']
);
$T->assign('giftArray', $giftArray);
$T->assign('arrRes', $arrRes);
$T->display('email.html');

